# 获取值
function get_cmt_value(s){
    file="YML_VAR.sh" NR
    cmd="sh " file
    rmcmd="rm " file

    printf("YML_VAR=%s\n", s)     > file
    printf("echo \"$YML_VAR\"\n") >> file
    cmd | getline YML_VAR
    system(rmcmd)

    return YML_VAR
}



function get_abs_key(paths, root, sk){
    idx = root - root/2
    if (root == 0) {
        delete paths
    } else {
        len = length(paths)
        for(i=0;i<len;i++){
            if (i > idx) {
                delete paths[idx+1]
            }
        }
    }
    paths[idx] = sk
    len = length(paths)
    p=""
    for(i=0;i<len;i++){
        p = i == 0 ? paths[i] : p "." paths[i]
    }
    #print "root: " root ", idx: "idx ", len:" length(paths)
    return p
}

function ltrim(s){
    sub(/[[:blank:]]*/, "", s)
    return s
}

function get_key(s){
    # Get key
    #   kind: 'console123      #' #abc
    idx = index(s, ": ")
    if (idx > 0) {
        sk=substr(s, 0, idx-1)
    } else {
        #   requests:
        if ($0 ~ /:$/) {
            # 当前行无值
            idx = length(s)
            sk=substr(s, 0, idx-1)
        } else {
            # 只有值，比如数组元素
            sk = ""
        }
    }
    return ltrim(sk)
}

function get_value(s){
    # 内行存在注释
    if (index(s, "#") > 0) {
        return get_cmt_value(s)
    }
    return s
}


function is_array_open(s){
    return (s ~ /^[[:blank:]]*-/)
}

# function inline_comment(s){
# }



BEGIN{
    root=0
    oper=modify "" remove
    matcher="root.appenders.1"
    new_value="abc"
    comment="ABCABC"

    print get_key("       kind: 'console123      #' #abc")
}

$0 ~ /^[[:blank:]]*#/ || NF == 0 {
    print $0
    next
}

{

    # if is_array_open($0){

    # }

    # Get key
    idx = index($0, ": ")
    if (idx > 0) {
        sk=substr($0, 0, idx-1)
        new_sk=substr($0, 0, idx+1)
        sub(/[[:blank:]]*/, "", new_sk)
    } else {
        if ($0 ~ /:$/) {
            # 当前行无值
            idx = length($0)
            sk=substr($0, 0, idx-1)
            new_sk=substr($0, 0, idx+1)
            sub(/[[:blank:]]*/, "", new_sk)
        } else {
            # 只有值，比如数组元素
            #value=get_value($0) 
            #print "value====>" value
        }
    }

    # 空格数
    root = index($0, $1) - 1
    if (root == 0) {
        prefix=sk
        path=get_abs_key(paths, root, sk)
    } else {
        # 其他级别
        sub(/[[:blank:]]*/, "", sk)
        prefix=prefix"."sk
        path=get_abs_key(paths, root, sk)
    }

    spaces=substr($0, 0, root)
    sub(spaces "" new_sk, "", $0)

    # 数组
    if ($0 ~ /^[[:blank:]]*-/) {
        is_array=true
        sub(/^[[:blank:]]*- /, "", $0)
        #print "is-ARRAY: "$0
        if (oper == "00") {
            # get
            value=get_value($0) 
            if(arr_idx=="") arr_idx=0
            values[arr_idx]=value
            path = path "[" arr_idx "]"
            arr_idx+=1
        } else if (oper == "10") {
            # modify
        } else if (oper == "01") {
            # remove
        }

        
    } else {
        if (is_array) {
            # 上一行是数组
        }
        is_array=false
        # 数组
        # if (oper == "00") {
        #     l=length(values)
        #     if (l > 0) {
        #         for(i=0;i<l;i++) {
        #             print "" path "[" i "]=" values[i]
        #         }
        #         delete values
        #     }
        # }

        arr_idx=0
        if (index($0, "#") == 0){
            # Non Comment
            value=$0
        } else {
            value=get_value($0) 
        }

    }

    print "#" NR ", path=" path ", value=`"value"`"
    

}